package edu.buu.refectory.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.buu.refectory.constant.Const;
import edu.buu.refectory.domain.User;
import edu.buu.refectory.service.UserService;
import edu.buu.refectory.utils.CommonUtil;

@RequestMapping(value="/users")
@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	
	@RequestMapping(method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request){
        Map<String,Object> map = CommonUtil.getParameterMap(request);
		return new ResponseEntity<Object>(userService.getList(map), HttpStatus.OK);
    }
	
//	@RequestMapping(value="/{id}", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
//	@ResponseBody
//    public ResponseEntity<?> detail(@PathVariable Integer id){
//		return new ResponseEntity<Object>(userService.getById(id), HttpStatus.OK);
//    }
    
    @RequestMapping(method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	@ResponseBody
    public ResponseEntity<?> create(User user,HttpServletRequest request){
        List<User> oldUserList = userService.getByUsername(user.getuName());
        if(oldUserList != null && oldUserList.size() > 0){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(Const.ERROR_MESSAGE, "登录名已存在!");
            return new ResponseEntity<Object> (map, HttpStatus.CREATED);
        }else{
            user = userService.create(user);
            return new ResponseEntity<Object> (user, HttpStatus.CREATED);
        }
    }
    
//    @RequestMapping(method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
//	@ResponseBody
//    public ResponseEntity<?> update(@RequestBody User user, HttpServletRequest request){
//        user.setUpdateBy(CommonUtil.getCurrentUsername(request));
//        user = userService.synUser(user);
//		return new ResponseEntity<Object>(user, HttpStatus.OK);
//    }
    
//    @RequestMapping(value="/{id}", method = RequestMethod.DELETE)
//	@ResponseBody
//    public ResponseEntity<?> delete(@PathVariable Integer id){
//        userService.deleteUserAndCorrelationInfo(id);
//		return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
//    }
//    
//    @RequestMapping(value="/updateAllPasswords", method = RequestMethod.GET)
//    @ResponseBody
//    public ResponseEntity<?> updateAllPasswords(HttpServletRequest request){
//        Map<String,Object> map = CommonUtil.getParameterMap(request);
//        @SuppressWarnings("unchecked")
//        List<User> userList = (List<User>) userService.getList(map);
//        if(userList != null && userList.size() > 0){
//            for(User user : userList){
//                user.setRePassword("888888");
//                userService.update(user);
//            }
//        }
//        return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
//    }
    
}